Page({
    data: {
      // selectedOption: "Popular", // 默认选中选项为Popular
      selectedArtistOption: {}, // 艺术家默认选中选项为空
      selectedCategoryOption: {}, // 类别默认选中选项为空
      selectedMovementOption: {}, // 流派默认选中选项为空
      selectedTopicOption: {}, // 话题默认选中选项为空
      PopularOption: true, //热门选项默认选中
      /* options: [
        { name: "Popular", icon: "/static/icons/select.png" },
        { name: "ARTIST", icon: "/static/icons/right-arrow.png" },
        { name: "CATEGORY", icon: "/static/icons/right-arrow.png" },
        { name: "MOVEMENT", icon: "/static/icons/right-arrow.png" },
        { name: "TOPIC", icon: "/static/icons/right-arrow.png" },
      ], */
    },

    async onLoad() {
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.on("sendDataToChild", (data) => {
        //有数据，但是内容也不为空
          if (data.selectedArtistOption.id || data.selectedCategoryOption.id || data.selectedMovementOption.id || data.selectedTopicOption.id) {
              this.setData({
                selectedArtistOption: data.selectedArtistOption, 
                selectedCategoryOption: data.selectedCategoryOption, 
                selectedMovementOption: data.selectedMovementOption, 
                selectedTopicOption: data.selectedTopicOption, 
                PopularOption: data.PopularOption
              });
          }
          //有数据，但是内容也不为空
          // if(data.selectedArtistOption.id || data.selectedCategoryOption.id || data.selectedMovementOption.id || data.selectedTopicOption.id){
          //   this.setData({
          //     selectedOption: null, // 清空选中状态
          //   });
          // }
          console.log(
            "过滤子页面接收到数据:",
            this.data.PopularOption,
          );
          console.log(
              "过滤子页面接收到艺术家数据:",
              this.data.selectedArtistOption.id,
              "-",
              this.data.selectedArtistOption.artistName
          );
          console.log(
            "过滤子页面接收到类别数据:",
            this.data.selectedCategoryOption.id,
            "-",
            this.data.selectedCategoryOption.categoryName
          );
          console.log(
            "过滤子页面接收到流派数据:",
            this.data.selectedMovementOption.id,
            "-",
            this.data.selectedMovementOption.movementName
          );
          console.log(
            "过滤子页面接收到话题数据:",
            this.data.selectedTopicOption.id,
            "-",
            this.data.selectedTopicOption.topicName
          );
      });
    },
  
    // 切换到Popular选项
    selectPopular() {
        if (this.data.PopularOption) {
        // 如果当前已经选中Popular，再次点击取消选中
        this.setData({
          PopularOption: false, // 清空选中状态
            // "options[0].icon": "/static/icons/select.png", // 恢复默认图标
        });
        } else {
        // 设置为选中Popular
        this.setData({
          PopularOption: true,
            // "options[0].icon": "/static/icons/select_active.png", // 切换选中图标
        });
        // 其他选项的图标复原
        // this.resetIcons(0);
        }
    },
  
  
    // 切换到ARTIST选项
    selectArtist() {
      /* this.setData({
        selectedOption: "ARTIST",
      }); */
      wx.navigateTo({
        url: "/pages/filter/artist/artist", // 跳转到子选项页面
        events: {
          acceptDataFromChild: (data) => {
            console.log("从艺术家选项子页面接收到的数据:", data);
            this.setData({
              selectedArtistOption: data.selectedArtistOption, // 更新父页面的数据
            });
          },
        },
        success: (res) => {
          // 使用 eventChannel 传递数据
          res.eventChannel.emit("sendDataToChild", {
            selectedArtistOption: this.data.selectedArtistOption,
          });
        },
      });
    },
    
  
    // 切换到CATEGORY选项
    selectCategory() {
      /* this.setData({
        selectedOption: "CATEGORY",
      }); */
      wx.navigateTo({
        url: "/pages/filter/category/category", // 跳转到子选项页面
        events: {
          acceptDataFromChild: (data) => {
            console.log("从类别选项子页面接收到的数据:", data);
            this.setData({
              selectedCategoryOption: data.selectedCategoryOption, // 更新父页面的数据
            });
          },
        },
        success: (res) => {
          // 使用 eventChannel 传递数据
          res.eventChannel.emit("sendDataToChild", {
            selectedCategoryOption: this.data.selectedCategoryOption,
          });
        },
      });
    },
  
    // 切换到MOVEMENT选项
    selectMovement() {
      /* this.setData({
        selectedOption: "MOVEMENT",
      }); */
      wx.navigateTo({
        url: "/pages/filter/movement/movement", // 跳转到子选项页面
        events: {
          acceptDataFromChild: (data) => {
            console.log("从流派选项子页面接收到的数据:", data);
            this.setData({
              selectedMovementOption: data.selectedMovementOption, // 更新父页面的数据
            });
          },
        },
        success: (res) => {
          // 使用 eventChannel 传递数据
          res.eventChannel.emit("sendDataToChild", {
            selectedMovementOption: this.data.selectedMovementOption,
          });
        },
      });
    },
  
    // 切换到TOPIC选项
    selectTopic() {
      /* this.setData({
        selectedOption: "TOPIC",
      }); */
      wx.navigateTo({
        url: "/pages/filter/topic/topic", // 跳转到子选项页面
        events: {
          acceptDataFromChild: (data) => {
            console.log("从话题选项子页面接收到的数据:", data);
            this.setData({
              selectedTopicOption: data.selectedTopicOption, // 更新父页面的数据
            });
          },
        },
        success: (res) => {
          // 使用 eventChannel 传递数据
          res.eventChannel.emit("sendDataToChild", {
            selectedTopicOption: this.data.selectedTopicOption,
          });
        },
      });
    },
    // 清除 Popular 选项
    clearPopular() {
      this.setData({
        PopularOption: false, // 清空选中状态
        // "options[0].icon": "/static/icons/select.png", // 恢复默认图标
    });
    },

    // 清除 ARTIST 选项
    clearArtist() {
      this.setData({
        selectedArtistOption: {},
      });
    },
    // 清除 CATEGORY 选项
    clearCategory() {
      this.setData({
        selectedCategoryOption: {},
      });
    },

    // 清除 MOVEMENT 选项
    clearMovement() {
      this.setData({
        selectedMovementOption: {},
      });
    },

    // 清除 TOPIC 选项
    clearTopic() {
      this.setData({ selectedTopicOption: {} });
    },
  
    // 重置除当前选中外的图标
    /* resetIcons(excludeIndex) {
      const updatedOptions = this.data.options.map((option, index) => {
        if (index !== excludeIndex) {
          option.icon = "/static/icons/right-arrow.png"; // 恢复默认图标
        }
        return option;
      });
      this.setData({ options: updatedOptions });
    }, */
  
    // 应用筛选
    applyFilters() {
      const selectedArtistOption = this.data.selectedArtistOption;
      const selectedCategoryOption = this.data.selectedCategoryOption;
      const selectedMovementOption = this.data.selectedMovementOption;
      const selectedTopicOption = this.data.selectedTopicOption;
      const PopularOption = this.data.PopularOption;

      if (!selectedArtistOption.id &&  !selectedCategoryOption.id &&  !selectedMovementOption.id && !selectedTopicOption.id && !PopularOption) {
          wx.showModal({
              title: "Error",
              content: "Please select an option before applying.",
              showCancel: false,
          });
          return;
      }

      // 获取事件通道并发送数据
     const eventChannel = this.getOpenerEventChannel();
     eventChannel.emit("acceptDataFromChild", {
      selectedArtistOption,selectedCategoryOption,selectedMovementOption,selectedTopicOption,PopularOption
     });

     wx.navigateBack(); // 返回上一页
    },
  });  